package middleware

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"server/http/controller"
	"server/service"
)

func Auth() gin.HandlerFunc {
	return func(c *gin.Context) {

		token := c.GetHeader("token")
		fmt.Println("token-->", token)
		if token == "" {
			controller.ResultError(c, service.ErrNoToken)
			return
		}

		userModel, err := service.Us.UserDao.FindByToken(c, token)
		if err != nil {
			controller.ResultError(c, service.ErrNoToken)
			return
		}

		c.Set("user", userModel)
		// 请求前
		c.Next()
	}
}
